package com.demo.weixin.pdf;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;

public class FieldNameExtractor {
    public static void main(String[] args) {
        String tmp = "C:\\Users\\Administrator\\Pictures\\资料\\实验室安全教育考试合格证(横版2020).pdf";
        try (PdfDocument pdfDoc = new PdfDocument(new PdfReader(tmp))) {
            PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, false);

            if (form != null) {
                form.getAllFormFields().forEach((name, field) -> {
                    System.out.println("字段名称: " + name);
                    System.out.println("字段类型: " + field.getFormType());
                    System.out.println("字段坐标: " + field.getWidgets().get(0).getRectangle());
                    System.out.println("------------------------");
                });
            } else {
                System.out.println("该PDF未包含表单字段");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}